/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.romerito.app;

import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


/**
 *
 * @author Romerito Campos
 */
public class Menu extends Canvas implements Runnable{

    private Image imageMenu;

    private final int COLOR_MENU = 0x00ff0000;

    private static final int MAX_CPS = 50;
    
    private static final int MS_PER_FRAME = 1000/MAX_CPS;

    private int menuOption = 1;

    private int key = 0;

    

    public Menu() {
        
        try {
            imageMenu = Image.createImage("/TurtleFinal.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        setFullScreenMode(true);

        Thread thread = new Thread(this);
        thread.start();        
    }



    protected void paint(Graphics g) {
        g.setColor(0x00ccf2cc);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.drawImage(imageMenu, getXPosition(), getYPosition(), Graphics.TOP|Graphics.LEFT);

        
        String labels[] = {"Logo","Ajuda","Sair"};

        int y = (getYPosition()/2)+imageMenu.getHeight();
        int h = 5;
        for(int i=0;i<labels.length;i++){
            if(i==menuOption-1){
                g.setColor(COLOR_MENU);
                g.drawString(labels[i], getWidth()/2, y+h, Graphics.BASELINE|Graphics.HCENTER);
                h+=18;
            }else{
                g.setColor(0x0000009a);
                g.drawString(labels[i], getWidth()/2, y+h, Graphics.BASELINE|Graphics.HCENTER);
                h+=18;
            }
            
        }        
                            
    }    

    private int getXPosition(){
        if(getWidth()==128)
            return 0;
        else{
            int x = getWidth()-128;
            return x/2;
        }
    }

    private int getYPosition(){
        if(getHeight()==160){
            return 0;
        }else{
            int x = getHeight()-160;
            return x/3;
        }
    }
    

    public void run() {

        while(true){

            //lembra inicio
            long cycleStartTime = System.currentTimeMillis();

            long timeSinceStart = (cycleStartTime-System.currentTimeMillis());

            chooseMenu();
            repaint();
            
            if(timeSinceStart < MS_PER_FRAME){
                try {
                    Thread.sleep(MS_PER_FRAME - timeSinceStart);
                } catch (InterruptedException ex) {

                }
            }

            

        }
    }

    
    protected void keyPressed(int keyCode) {
        key = getGameAction(keyCode);
    }

    public void chooseMenu(){
               
        if(key == UP){
            menuOption--;
            if(menuOption<1)
                menuOption = 3;
        }

        if(key == DOWN){
            menuOption++;
            if(menuOption>3)
                menuOption = 1;
        }

        if(key == FIRE){
            switch(menuOption){
                case 1: MidletApp.app.showLogo();
                        Thread.yield();
                break;

                case 2: MidletApp.app.showHelp();
                break;

                case 3: MidletApp.app.destroyApp(true);
                break;
            }
        }

        key = 0;
    }

}
